package com.goods.business.service.imp;

import com.goods.business.converter.OutStockConverter;
import com.goods.business.mapper.*;
import com.goods.business.service.ConsumerService;
import com.goods.business.service.OutStockInfoService;
import com.goods.business.service.OutStockService;
import com.goods.common.model.business.*;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.OutStockDetailVO;
import com.goods.common.vo.business.OutStockItemVO;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

@Service
public class OutStockServiceImpl implements OutStockService {
    @Resource
    private OutStockMapper outStockMapper;
    @Resource
    private ConsumerService consumerService;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private OutStockInfoMapper outStockInfoMapper;
    @Resource
    private ConsumerMapper consumerMapper;
    @Resource
    private ProductStockMapper productStockMapper;
    //出库单分页列表
    @Override
    public PageVO<OutStockVO> findOutStockList(Map<String, Object> queryMap) {
        Example example = new Example(OutStock.class);
        if (queryMap.get("outNum") != null && !"".equals(queryMap.get("outNum"))) {
            example.createCriteria().andEqualTo("outNum", queryMap.get("outNum"));
        }
        if (queryMap.get("type") != null && !"".equals(queryMap.get("type"))) {
            example.createCriteria().andEqualTo("type", queryMap.get("type"));
        }
        if (queryMap.get("status") != null && !"".equals(queryMap.get("status"))) {
            example.createCriteria().andEqualTo("status", queryMap.get("status"));
        }
        List<OutStock> outStocks = outStockMapper.selectByExample(example);
        List<OutStockVO> outStockVOList = OutStockConverter.converterToOutStockVOList(outStocks);
        for (OutStockVO outStockVO : outStockVOList) {
             Consumer consumer = consumerService.getByid(outStockVO.getConsumerId());
             outStockVO.setName(consumer.getName());//发放地点
             outStockVO.setPhone(consumer.getPhone());
        }
        String pageSize = (String)queryMap.get("pageSize");
        String pageNum = (String)queryMap.get("pageNum");
        List<OutStock> page = ListPageUtils.page(outStocks, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
        return new PageVO<>(page.size(),outStockVOList);
    }
    //出库单的明细
    @Override
    public Map<Object, Object> detail(Long id, Integer pageNum) {
        //返回结果为个map
        Map<Object, Object> retMap = new HashMap<>();
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        Integer status = outStock.getStatus();
        retMap.put("status",status);

        ConsumerVO consumerVO = new ConsumerVO();
        Consumer consumer = consumerService.getByid(outStock.getConsumerId());
        BeanUtils.copyProperties(consumer,consumerVO);
        retMap.put("consumerVO",consumerVO);

        //根据出库记录的id获取 出库记录对应的相关信息 封装到OutStockItemVO中
        List<OutStockItemVO> outStockItemVOList = outStockMapper.selelctAboutInfoById(id);
        List<OutStockItemVO> page = ListPageUtils.page(outStockItemVOList, 5, pageNum);
        retMap.put("total",page.size());
        retMap.put("itemVOS",outStockItemVOList);
        return retMap;
    }

    @Override
    public void remove(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        if (outStock != null){
            outStock.setStatus(1);
            outStock.setId(id);
            outStockMapper.updateByPrimaryKey(outStock);
        }
    }

    @Override
    public void back(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        if (outStock != null){
            outStock.setStatus(0);
            outStock.setId(id);
            outStockMapper.updateByPrimaryKey(outStock);
        }
    }

    @Override
    public void delete(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        if (outStock != null){
            outStockMapper.delete(outStock);
        }
    }
    //添加出库
    @Override
    public void addOutStock(OutStockVO outStockVO,String username) {
        String outNum = UUID.randomUUID().toString();

        List<Object> products = outStockVO.getProducts();
        OutStockInfo outStockInfo = new OutStockInfo();
        for (Object object : products) {
            //将数据强转为map因为前端传的是 key value
            Map<String, Object> map = (Map<String, Object>)object;
            Object productId = map.get("productId");
            Integer productNumber = (Integer)map.get("productNumber");
            Product product = productMapper.selectByPrimaryKey(productId);
            String pNum = product.getPNum();

            //出库详情信息
            outStockInfo.setOutNum(outNum);
            outStockInfo.setPNum(pNum);
            outStockInfo.setCreateTime(new Date());
            outStockInfo.setModifiedTime(new Date());
            outStockInfo.setProductNumber(productNumber);
            outStockInfoMapper.insert(outStockInfo);
        }

        if (outStockVO.getConsumerId() == null){
            Consumer consumer = new Consumer();
            //物资去处信息
            consumer.setSort(outStockVO.getSort());
            consumer.setName(outStockVO.getName());
            consumer.setAddress(outStockVO.getAddress());
            consumer.setCreateTime(new Date());
            consumer.setModifiedTime(new Date());
            consumer.setPhone(outStockVO.getPhone());
            consumer.setContact(outStockVO.getContact());
            consumerMapper.insert(consumer);

            //出库记录信息
            OutStock outStock = new OutStock();
            outStock.setStatus(2);
            outStock.setOutNum(outNum);
            outStock.setType(1);
            outStock.setOperator(username);
            outStock.setCreateTime(new Date());
            outStock.setProductNumber(products.size());
            outStock.setConsumerId(consumer.getId());
            outStock.setRemark(outStockVO.getRemark());
            outStock.setPriority(2);
            outStockMapper.insert(outStock);
        }else {
            //出库记录信息
            OutStock outStock = new OutStock();
            outStock.setStatus(2);
            outStock.setOutNum(outNum);
            outStock.setType(1);
            outStock.setOperator(username);
            outStock.setCreateTime(new Date());
            outStock.setProductNumber(products.size());
            outStock.setConsumerId(outStockVO.getConsumerId());
            outStock.setRemark(outStockVO.getRemark());
            outStock.setPriority(2);
            outStockMapper.insert(outStock);
        }

    }
    //出库记录的审核
    @Override
    public void publish(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStock.setStatus(0);
        outStockMapper.updateByPrimaryKey(outStock);
        //减少库存
        String outNum = outStock.getOutNum();
        Example example = new Example(OutStockInfo.class);
        example.createCriteria().andEqualTo("outNum",outNum);
        List<OutStockInfo> outStockInfoList = outStockInfoMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(outStockInfoList)){
            for (OutStockInfo outStockInfo : outStockInfoList) {
                String pNum = outStockInfo.getPNum();
                Integer productNumber = outStockInfo.getProductNumber();
                Example example1 = new Example(ProductStock.class);
                example1.createCriteria().andEqualTo("pNum",pNum);
                List<ProductStock> productStocks = productStockMapper.selectByExample(example1);
                for (ProductStock productStock : productStocks) {
                    productStock.setStock(productStock.getStock()-productNumber);
                    productStockMapper.updateByPrimaryKey(productStock);
                }
            }
        }
    }
}
